import axios from "axios";

import { ElLoading, ElMessage } from "element-plus";

import { getToken } from '@/utils/cookie.js'
const service = axios.create({
    // baseURL:import.meta.env.ENV_BASE_API,
    baseURL:'/mock',
    timeout: 3000
})

let loadingInstance;
service.interceptors.request.use(

    config => {
        if(getToken()){
            config.headers['token']=getToken()
        }
        loadingInstance = ElLoading.service("加载中");
        return config
    },
    error => {
        loadingInstance.close();
        ElMessage.error("网络异常");
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        loadingInstance.close();

        return response.data
    },
    error => {
        loadingInstance.close();
        ElMessage.error("请求失败")
        return Promise.reject(error)
    }
)

export default service